/**
 * 
 */
package com.manage.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;


import com.action.AbstractAction;
import com.manage.cache.AppPointOfBillingManager;
import com.manage.cache.AppSdkManager;
import com.manage.cache.CpAppInfoManager;
import com.manage.cache.CpInfoManager;
import com.manage.cache.PointSdkConfigManager;
import com.manage.cache.SdkParamaterManager;
import com.manage.cache.SdkProvidersManager;
import com.manage.model.AppPointOfBilling;
import com.manage.model.AppSdk;
import com.manage.model.CpAppInfo;
import com.manage.model.CpInfo;
import com.manage.model.PointSdkConfig;
import com.manage.model.SdkParamater;
import com.manage.model.SdkProviders;
import com.util.Util;


public class AppPointOfBillingAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger logger;
	
	public AppPointOfBillingAction()
	{
		logger = Logger.getLogger(this.getClass());
	}
	
	public String execute()
	{
		return SUCCESS;
	}

	private List<AppPointOfBilling> list;
	private AppPointOfBilling       model;
	private String        type;
	private CpAppInfo appinfo;
	private CpInfo cpinfo;
	private List<SdkProviders> sdkplist;
	
	private List<PointSdkConfig> psclist;
	public String list()
	{
		logger.debug("list AppPointOfBillingAction");
		cpinfo = CpInfoManager.getInstance().getCpInfo(cpinfo.getId());
		appinfo = CpAppInfoManager.getInstance().getCpAppInfoByAppNumber(model.getAppnumber());
		list = AppPointOfBillingManager.getInstance().getAppPointOfBillingByAppnumber(model.getAppnumber());
		return SUCCESS;
	}
	
	public String show()
	{
		sdkplist = new ArrayList<SdkProviders>();
		List<AppSdk> aslist=AppSdkManager.getInstance().getAppSdkByAppnumber(model.getAppnumber());
		for(int i =0;i<aslist.size();i++)
		{
			if(aslist.get(i).getSdkid()!=1000)
				sdkplist.add(SdkProvidersManager.getInstance().getSdkProviders(aslist.get(i).getSdkid()));
		}
		cpinfo = CpInfoManager.getInstance().getCpInfo(cpinfo.getId());
		appinfo = CpAppInfoManager.getInstance().getCpAppInfoByAppNumber(model.getAppnumber());
		if (model.getId() >= 0)
		{
			model = AppPointOfBillingManager.getInstance().getAppPointOfBilling(model.getId());
			psclist = PointSdkConfigManager.getInstance().getPointSdkConfigByPointofbillingid(model.getId());
			type = "update";
		}
		else
		{
			model.setId(-1);
			type = "insert";
		}

		return SUCCESS;
	}
	
	public String save()
	{
		cpinfo = CpInfoManager.getInstance().getCpInfo(cpinfo.getId());
		appinfo = CpAppInfoManager.getInstance().getCpAppInfoByAppNumber(model.getAppnumber());
		
		model.setCpid(cpinfo.getId());
		if(type.equals("insert"))
		{
			model.setCreatedate(Util.dateToString(new Date()));
			model.setCreateuser(operator.getName());
			//判断code是否存在，存在就直接返回
			if(AppPointOfBillingManager.getInstance().getAppPointOfBillingOfAppNumberCount(model.getAppnumber(),model.getCode()))
			{
				addActionError("计费点代码已经存在！");
				return SUCCESS;
			}
		}
		AppPointOfBillingManager.getInstance().saveAppPointOfBilling(operator, type, model);
		psclist = new ArrayList<PointSdkConfig>();
		List<AppSdk> aslist=AppSdkManager.getInstance().getAppSdkByAppnumber(model.getAppnumber());
		if(!"delete".equals(type))
		{
			for(int i =0;i<aslist.size();i++)
			{
				if(aslist.get(i).getSdkid()!=1000)
				{
					String str = getRequest().getParameter("sdkparamater_"+aslist.get(i).getSdkid());
					if(str!=null && !"".equals(str))
					{
						PointSdkConfig psc = new PointSdkConfig();
						psc.setAppnumber(model.getAppnumber());
						psc.setCpid(cpinfo.getId());
						psc.setPointofbillingid(model.getId());
						psc.setPrarmatervalue(str);
						psc.setSdkprovidersid(aslist.get(i).getSdkid());
						psclist.add(psc);
					}
				}
			}
		}
		PointSdkConfigManager.getInstance().saveAppPointOfBilling(psclist, model.getId());
		return SUCCESS;
	}
	
	public String getDefaultParamater(int sdkid)
	{
		String paramater = "";
		List<SdkParamater> pl = SdkParamaterManager.getInstance().getSdkParamaterBysdkid(sdkid);
		if(pl!=null && pl.size()>0)
		{
			for(int i=0;i<pl.size();i++)
			{
				if(i>0)
				{
					paramater+="#";
				}
				paramater+=pl.get(i).getHashkey()+"="+pl.get(i).getHashvalue();
			}
		}
		return paramater;
	}
	
	public List<PointSdkConfig> getPsclist() {
		return psclist;
	}

	public void setPsclist(List<PointSdkConfig> psclist) {
		this.psclist = psclist;
	}

	public List<SdkProviders> getSdkplist() {
		return sdkplist;
	}

	public void setSdkplist(List<SdkProviders> sdkplist) {
		this.sdkplist = sdkplist;
	}

	public List<AppPointOfBilling> getList() {
		return list;
	}

	public void setList(List<AppPointOfBilling> list) {
		this.list = list;
	}

	public AppPointOfBilling getModel() {
		return model;
	}

	public void setModel(AppPointOfBilling model) {
		this.model = model;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public CpAppInfo getAppinfo() {
		return appinfo;
	}

	public void setAppinfo(CpAppInfo appinfo) {
		this.appinfo = appinfo;
	}

	public CpInfo getCpinfo() {
		return cpinfo;
	}

	public void setCpinfo(CpInfo cpinfo) {
		this.cpinfo = cpinfo;
	}
	
}
